package com.example.launibleplugin.model

import com.google.gson.annotations.SerializedName

/**
 * 连接状态枚举
 * Function: 定义蓝牙连接的各种状态
 * Note: 用于状态管理和UI显示
 */
enum class ConnectionState {
    DISCONNECTED,    // 未连接
    CONNECTING,      // 连接中
    CONNECTED,       // 已连接
    DISCONNECTING,   // 断开连接中
    ERROR           // 连接错误
}

/**
 * 连接信息数据模型
 * Function: 表示蓝牙设备的连接信息
 * Parameter: 连接状态、时间戳、错误信息等
 * Note: 用于连接管理和状态跟踪
 */
data class ConnectionInfo(
    @SerializedName("connection_id")
    val connectionId: String,
    
    @SerializedName("device_id")
    val deviceId: String,
    
    @SerializedName("state")
    val state: ConnectionState,
    
    @SerializedName("connect_time")
    val connectTime: Long = System.currentTimeMillis(),
    
    @SerializedName("disconnect_time")
    val disconnectTime: Long? = null,
    
    @SerializedName("signal_strength")
    val signalStrength: Int = 0,
    
    @SerializedName("error_message")
    val errorMessage: String? = null,
    
    @SerializedName("auto_reconnect")
    val autoReconnect: Boolean = true,
    
    @SerializedName("reconnect_attempts")
    val reconnectAttempts: Int = 0,
    
    @SerializedName("max_reconnect_attempts")
    val maxReconnectAttempts: Int = 3
) {
    /**
     * Function: 获取连接持续时间
     * Return: Long - 连接持续时间（毫秒）
     * Note: 如果未断开连接，返回当前时间与连接时间的差值
     */
    fun getConnectionDuration(): Long {
        val endTime = disconnectTime ?: System.currentTimeMillis()
        return endTime - connectTime
    }
    
    /**
     * Function: 判断是否可以重连
     * Return: Boolean - 是否可以重连
     * Note: 检查自动重连设置和重连次数限制
     */
    fun canReconnect(): Boolean {
        return autoReconnect && reconnectAttempts < maxReconnectAttempts
    }
    
    /**
     * Function: 获取状态描述
     * Return: String - 状态文本描述
     * Note: 将枚举状态转换为用户友好的中文描述
     */
    fun getStateDescription(): String {
        return when (state) {
            ConnectionState.DISCONNECTED -> "未连接"
            ConnectionState.CONNECTING -> "连接中"
            ConnectionState.CONNECTED -> "已连接"
            ConnectionState.DISCONNECTING -> "断开连接中"
            ConnectionState.ERROR -> "连接错误"
        }
    }
}